import 'package:flutter/material.dart';
import 'package:flutter_bilibili/core/hi_base_tab_state.dart';
import 'package:flutter_bilibili/http/dao/home_dao.dart';
import 'package:flutter_bilibili/model/home_model.dart';
import 'package:flutter_bilibili/model/video_model.dart';
import 'package:flutter_bilibili/widget/hi_banner.dart';
import 'package:flutter_bilibili/widget/video_card.dart';
import 'package:flutter_nested/flutter_nested.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class HomeTabPage extends StatefulWidget {
  final String categoryName;
  final List<BannerMo> bannerList;
  const HomeTabPage({Key key, this.categoryName, this.bannerList})
      : super(key: key);

  @override
  _HomeTabPageState createState() => _HomeTabPageState();
}

class _HomeTabPageState
    extends HiBaseTabState<HomeModel, VideoModel, HomeTabPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print(widget.categoryName);
  }

  _banner() {
    // return Padding(
    //   padding: EdgeInsets.only(left: 5, right: 5),
    //   child: HiBanner(widget.bannerList,padding: EdgeInsets.only(left: 10, right: 10)),
    // );
    return HiBanner(widget.bannerList,
        padding: EdgeInsets.only(left: 10, right: 10));
  }

  // @override
  // get contentChild => StaggeredGridView.countBuilder(
  //     controller: scrollController,
  //     physics: const AlwaysScrollableScrollPhysics(),
  //     padding: EdgeInsets.only(top: 10, left: 10, right: 10),
  //     crossAxisCount: 2,
  //     itemCount: dataList.length,
  //     itemBuilder: (BuildContext context, int index) {
  //       //  有banner时第一个item位置显示banner
  //       if (widget.bannerList != null && index == 0) {
  //         return Padding(padding: EdgeInsets.only(bottom: 8), child: _banner());
  //       } else {
  //         return VideoCard(
  //           videoMo: dataList[index],
  //         );
  //       }
  //     },
  //     staggeredTileBuilder: (int index) {
  //       if (widget.bannerList != null && index == 0) {
  //         return StaggeredTile.fit(2);
  //       } else {
  //         return StaggeredTile.fit(1);
  //       }
  //     });

  @override
  get contentChild => HiNestedScrollView(
      controller: scrollController,
      itemCount: dataList.length,
      padding: EdgeInsets.only(top: 10, left: 10, right: 10),
      headers: [
        if (widget.bannerList != null)
          Padding(padding: EdgeInsets.only(bottom: 8), child: _banner())
      ],
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2, childAspectRatio: 0.95),
      itemBuilder: (BuildContext context, int index) {
        return VideoCard(
          videoMo: dataList[index],
        );
      });

  @override
  Future<HomeModel> getData(int pageIndex) async {
    HomeModel result = await HomeDao.get(widget.categoryName,
        pageIndex: pageIndex, pageSize: 10);
    return result;
  }

  @override
  List<VideoModel> parseList(HomeModel result) {
    return result.videoList;
  }
}
